package com.hryk.medical.common.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hryk.medical.R;

/**
 * Author: admin
 * Date: 2020/3/20
 */
public class TopNavigationBar extends LinearLayout {

    private TabLayout mTab;
    private TextView userName;
    private TextView weatherTv;
    private TextView timeTv;

    private TabViewHolder mHolder;

    public TopNavigationBar(Context context) {
        super(context);
        initView(context);
    }

    public TopNavigationBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public TopNavigationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public TopNavigationBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        View view = inflate(getContext(), R.layout.view_top_navigation_bar, this);
        mTab = view.findViewById(R.id.top_navigation_tab);
        userName = view.findViewById(R.id.top_navigation_user_name);
        weatherTv = view.findViewById(R.id.top_navigation_weather);
        timeTv = view.findViewById(R.id.top_navigation_time);

        findViewById(R.id.top_navigation_user_ll).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
        findViewById(R.id.top_navigation_setting).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    public void setUserName(String name) {
        if (userName != null && !TextUtils.isEmpty(name)) {
            userName.setText(name);
        }
    }

    public void setWeather(String weather) {
        if (weatherTv != null && !TextUtils.isEmpty(weather)) {
            weatherTv.setText(weather);
        }
    }

    public void setTime(String time) {
        if (timeTv != null && !TextUtils.isEmpty(time)) {
            timeTv.setText(time);
        }
    }

    public void setupWithViewPager(final ViewPager mViewPager) {
        if (mTab == null || mViewPager == null) {
            return;
        }
        mTab.setupWithViewPager(mViewPager);
        setupTabView(mTab);
        mTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTab(tab, true);
                mViewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                changeTab(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    private void setupTabView(TabLayout tabLayout) {
        String[] titles = new String[]{getContext().getString(R.string.main_tab_medical_service), getContext().getString(R.string.main_tab_health_education),
                getContext().getString(R.string.main_tab_video_fun), getContext().getString(R.string.main_tab_life_service), getContext().getString(R.string.main_tab_health_service)};
        int[] images = new int[]{R.mipmap.tab_medical_service_icon, R.mipmap.tab_health_education_icon, R.mipmap.tab_video_fun_icon
                , R.mipmap.tab_life_service_icon, R.mipmap.tab_health_service_icon};
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(R.layout.item_main_tab);
                View view = tab.getCustomView();
                if (view != null) {
                    mHolder = new TabViewHolder(view);
                    mHolder.tabTv.setText(titles[i]);
                    mHolder.tabImage.setImageResource(images[i]);
                }
            }
            if (i == 0) {
                mHolder.tabLl.setSelected(true);
            }
        }
    }

    private void changeTab(TabLayout.Tab tab, boolean isSelected) {
        View customView = tab.getCustomView();
        if (customView == null) {
            return;
        }
        mHolder = new TabViewHolder(customView);
        mHolder.tabTv.setSelected(isSelected);
    }

    static class TabViewHolder {
        LinearLayout tabLl;
        TextView tabTv;
        ImageView tabImage;

        TabViewHolder(View tabView) {
            tabLl = tabView.findViewById(R.id.main_tab_ll);
            tabTv = tabView.findViewById(R.id.main_tab_text);
            tabImage = tabView.findViewById(R.id.main_tab_image);
        }
    }
}
